package org.pictual.controller.mainPackage;

import java.io.BufferedReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class WikiDicParser2 extends MyParser {

    private static List<String> _typesEng = Arrays.asList(
            "Particle",
            "Noun",
            "Preposition",
            "Verb",
            "Adjective"
            );

   private static List<String> _typesEsp = Arrays.asList(
            "Sustantivo_femenino",
            "Sustantivo_masculino",
            "Verbo");

   private static Vector<List<String>> _types;
   static{
       _types = new Vector<List<String>>();
       _types.addElement(_typesEng);
       _types.addElement(_typesEsp);
   }

    private String _language;
    private int _langInt;

    private CategoriesWrapper _categs;
    private Vector<String> _otherLans;

    private BufferedReader input;
    private boolean _insideLan;
    private String pTag = "<p><a name=\"";
    private int _hIndex;

    private boolean isType(String word){
        List<String> v = _types.elementAt(_langInt);
        Iterator<String> iter = v.iterator();
        while(iter.hasNext())
        {
            String s = iter.next();
            if(word.startsWith(s)){
                return true;
            }
        }
        return false;
    }

    public WikiDicParser2(BufferedReader input, String lan) {
        _categs = new CategoriesWrapper();
        _otherLans = new Vector<String>();
        _language = lan;
        _langInt = Language.LangIndex(_language);
        this.input = input;
        _insideLan = false;
    }

    String WikiLang() {
        if (_language.equals("EspaÃ±ol"))
        {
            return "Espa.C3.B1ol";
        }
        return _language;
    }

    public CategoriesWrapper GetCategs() {
        return _categs;
    }

    public Vector<String> GetOtherLanguages() {
        return _otherLans;
    }

    private DefinitionList extractDefinitionList(DefinitionList listdef) {
        /*
        <ol>
        <li> we want </li>
        <li> we want
        <ul> <li> we dont want </li> </ul>
        </li>
        </ol>
         */
        boolean insideOl = false;
        boolean insideLi = false;
        boolean insideUl = false;

        String tmpDefinition = "";
        String line;
        try {
            while ((line = input.readLine()) != null) {
                if (insideLi && line.startsWith("</ul>")) {
                    insideUl = false;
                } else if (insideLi && line.startsWith("<ul>")) {
                    insideUl = true;
                } else if (insideOl && !insideUl && line.startsWith("<li>")) {
                    insideLi = true;
                    tmpDefinition = "";
                    if (line.endsWith("</li>")) {
                        insideLi = false;
                        tmpDefinition = line.substring(4, line.length() - 5);
                        listdef.addDefinition(tmpDefinition);
                    } else {
                        tmpDefinition += line.substring(4);
                    }
                } else if (line.startsWith("<ol>")) {
                    insideOl = true;
                } else if (insideLi && !insideUl && line.startsWith("</li>")) {
                    insideLi = false;
                    listdef.addDefinition(tmpDefinition);
                } else if (line.startsWith("</ol>")) {
                    return listdef;
                } else if (insideLi && !insideUl) {
                    if (line.endsWith("</li>")) {
                        insideLi = false;
                        tmpDefinition += line.substring(0, line.length() - 5);
                        listdef.addDefinition(tmpDefinition);
                    } else {
                        tmpDefinition += line;
                    }

                }
            }
        } catch (Exception e) {
        }
        return null;
    }

    public static String resolveName(String name) {
        int index = name.indexOf('_');
        String rname = name;
        if(index != -1){
            rname = name.substring(0,index);
        }
        return rname;
    }
    public void Parse() {
        /*
        <p><a name="English" id="English"></a></p>
        <p><a name="Etymology" id="Etymology"></a></p>
        <p><a name="Pronunciation" id="Pronunciation"></a></p>
        <p><a name="Noun" id="Noun"></a></p>
        <ol>
        </ol>
        <p><a name="Synonyms" id="Synonyms"></a></p>
        <p><a name="Derived_terms" id="Derived_terms"></a></p>
        <p><a name="Related_terms" id="Related_terms"></a></p>
        <p><a name="Translations" id="Translations"></a></p>
        <p><a name="External_links" id="External_links"></a></p>
        <p><a name="Verb" id="Verb"></a></p>
        <ol>
        </ol>
         */
        String line;
        try {
            while ((line = input.readLine()) != null) {
                if (line.startsWith(pTag)) {
                    //String nextLine = input.readLine();
                    String name = extractField(line, pTag.length());
                    if (_otherLans.isEmpty() && name.equals(WikiLang())) {
                        _insideLan = true;
                        String nextline = input.readLine();
                        _hIndex = Integer.parseInt(nextline.substring(2,3));

                    } else if (!_insideLan && Language.isLang(_langInt, name)) {
                        _otherLans.addElement(name);
                    } else if (_insideLan) {
                        String nextline = input.readLine();
                        int index = Integer.parseInt(nextline.substring(2,3));
                        if(index == _hIndex)
                        {
                            return;
                        } else if (isType(name)) {
                            name = resolveName(name);
                            DefinitionList list = _categs.getDefinitionList(name);
                            System.out.println("WikiDikParser2[1]:"+list.getDefinition(0));

                            extractDefinitionList(list);
                        }
                    }
                }
            }
        } catch (Exception e) {
        }


    }
}
